package com.example.yuejianapp14.Activity.Activity;

import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.yuejianapp14.Activity.Servics.UserService;
import com.example.yuejianapp14.R;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class RegActivity extends AppCompatActivity {

    private Button Reg;
    private Button Bac;
    private EditText phone,psd,uname,uaddress,uinfo;
    private ImageView regpic;
    private String path=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);

        initview();

        Reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Register();
            }
        });


        Bac.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RegActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        });

        regpic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final CharSequence[] items = {"相册"};
                new AlertDialog.Builder(RegActivity.this).setTitle("选择获取头像方式").setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (which==0){
                            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                            intent.setType("image/*");
                            startActivityForResult(intent,1);
                        }
                    }
                }).create().show();
            }
        });

    }


    public void initview(){
         Reg=(Button)findViewById(R.id.Register);
         Bac=(Button)findViewById(R.id.Bac2);
        phone=(EditText) findViewById(R.id.phone);
        psd=(EditText) findViewById(R.id.psd);
        uname=(EditText) findViewById(R.id.uname);
        uaddress=(EditText) findViewById(R.id.uaddress);
        uinfo=(EditText) findViewById(R.id.uinfo);
        regpic=(ImageView)findViewById(R.id.regpic);
    }

    void Register(){
        String name,pass,username,address,userinfo;
        name = phone.getText().toString();
        pass = psd.getText().toString();
        username = uname.getText().toString();
        address = uaddress.getText().toString();
        userinfo = uinfo.getText().toString();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://10.0.2.2/YueJian/index.php/Api/User/")
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
        UserService userService=retrofit.create(UserService.class);
        File file = new File(path);
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("pic", file.getName(), requestFile);
        Call<String> call=userService.register(name,pass,username,address,userinfo,body);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (!response.body().equals("0")){
                    Toast.makeText(RegActivity.this, "注册成功！", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(RegActivity.this,LoginActivity.class);
                    startActivity(intent);
                    finish();

                }else{
                    Toast.makeText(RegActivity.this,"注册失败！", Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Toast.makeText(RegActivity.this, "访问服务器失败！", Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 1:
                if (data!=null){
                    Uri uri = data.getData();
                    regpic.setImageURI(uri);
                    ContentResolver cr = this.getContentResolver();
                    Cursor c = cr.query(uri, null, null, null, null);
                    c.moveToFirst();
                    //这是获取的图片保存在sdcard中的位置
                    path = c.getString(c.getColumnIndex("_data"));
                    //Toast.makeText(RegActivity.this, path, Toast.LENGTH_SHORT).show();
                }else {
                    return;
                }
                break;
        }
    }
}
